package com.dhnsoft.代码随想录.链表;

/**
 * ClassName: d07_142_环形链表Ⅱ
 * Package: com.dhnsoft.代码随想录.链表
 * Description:
 *
 * @Author dhn
 * @Create 2024/3/11 12:55
 * @Version 1.0
 */
public class d07_142_环形链表Ⅱ {


    public static void main(String[] args) {

    }

    public static ListNode detectCycle(ListNode head,int pos) {
        if(head==null||head.next==null){
            return null;
        }
        ListNode newNode = new ListNode(-1);
        newNode.next = head;
        ListNode fast = newNode;
        ListNode slow = newNode;
        while (fast!=null){
            fast = fast.next.next;
            slow = slow.next;
            //进入环形
            if(slow==fast){
                ListNode temp = newNode;
                while (temp!=fast){
                    temp = temp.next;
                    fast = fast.next;
                }
                return fast;
            }
        }
        return null;

    }
}

